package com.beerandjesus.sportofkings.handicapper.strategy;

import com.beerandjesus.sportofkings.handicapper.model.ClassRating;
import com.beerandjesus.sportofkings.handicapper.model.ClassRatingImpl;

import java.util.List;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class ClassRatingResolver {
    private static final BigDecimal TWO = new BigDecimal(2);
    private static final BigDecimal ONE_HUNDRED = new BigDecimal(100);

    public ClassRating getClassRating(List<Integer> purses) {
        ClassRatingImpl r = new ClassRatingImpl();
        int raceCount = purses.size();
        r.setRaceCount(raceCount);
        Float ratingLastTwo = -1.0f;
        Float ratingAll = -1.0f;
        if (raceCount > 1) {
            Integer purse1 = purses.get(0);
            Integer purse2 = purses.get(1);
            BigDecimal sum = new BigDecimal(purse1 + purse2);
            r.setAveragePurseLastTwo(sum.divide(TWO).intValue());
            ratingLastTwo = sum.divide(ONE_HUNDRED).divide(TWO).floatValue();
            BigDecimal sumAll = new BigDecimal(0);
            for (Integer purse : purses) {
                if (purse > 0) {
                    sumAll = sumAll.add(new BigDecimal(purse));
                }
            }
            ratingAll = sumAll.divide(ONE_HUNDRED).divide(new BigDecimal(raceCount), RoundingMode.HALF_EVEN).floatValue();
        }
        r.setRatingLastTwo(ratingLastTwo);
        r.setRatingAll(ratingAll);
        return r;
	}

}
